package com.nopay.cdxphone.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author：jun
 * date：2021/7/30
 */
public class CallLogHelper {

    private static int max_id = -1;

    @SuppressLint("SimpleDateFormat")
    public static CallLogItem fetchNewCallLog(Context context) {
        if(context == null) return null;
        CallLogItem callLogItem = null;
        Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI, null, "_id > ?", new String[]{String.valueOf(max_id)}, CallLog.Calls.DEFAULT_SORT_ORDER);
        if(cursor != null) {
            Log.e("SIM_INFO", (cursor.getCount()) + "");
            if(cursor.moveToFirst()) {
                int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
                String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
                int duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION));
                int callType = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
                String numberType = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NUMBER_TYPE));

                String dataStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(date));
                callLogItem = new CallLogItem(id, number, dataStr, duration, callType, name, numberType);
                max_id = id;
            }
            cursor.close();
        }
        return callLogItem;
    }

    public static class CallLogItem {

        private int id;
        private String number;
        private String date;
        private int duration;
        private int callType;
        private String name;
        private String numberType;

        public CallLogItem(int id, String number, String date, int duration, int callType, String name, String numberType) {
            this.id = id;
            this.number = number;
            this.date = date;
            this.duration = duration;
            this.callType = callType;
            this.name = name;
            this.numberType = numberType;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public int getDuration() {
            return duration;
        }

        public void setDuration(int duration) {
            this.duration = duration;
        }

        public int getCallType() {
            return callType;
        }

        public void setCallType(int callType) {
            this.callType = callType;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumberType() {
            return numberType;
        }

        public void setNumberType(String numberType) {
            this.numberType = numberType;
        }
    }
}
